package com.opengroupe.dao.impl;

// Generated 31 juil. 2012 15:44:44 by Hibernate Tools 3.4.0.CR1

import java.util.List;

import javax.ejb.Stateless;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import com.opengroupe.dao.CategorieDao;
import com.opengroupe.entities.Categorie;

/**
 * Home object for domain model class Categorie.
 * @see com.opengroupe.entities.Categorie
 * @author Hibernate Tools
 */
@Stateless
public class CategorieDaoImpl extends JpaDaoSupport implements CategorieDao{

	private static final Log log = LogFactory.getLog(CategorieDaoImpl.class);

	public Categorie persist(Categorie transientInstance) {
		log.debug("persisting Categorie instance");
		try {
			getJpaTemplate().persist(transientInstance);
			log.debug("persist successful");
			return transientInstance;
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void remove(Categorie persistentInstance) {
		log.debug("removing Categorie instance");
		try {
			getJpaTemplate().remove(persistentInstance);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public Categorie merge(Categorie detachedInstance) {
		log.debug("merging Categorie instance");
		try {
			Categorie result = getJpaTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Categorie findById(Integer id) {
		log.debug("getting Categorie instance with id: " + id);
		try {
			Categorie instance = getJpaTemplate().find(Categorie.class, id);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	@SuppressWarnings("unchecked")
	public Categorie findByLibelle(String libelle) {
		String query = "select c from Categorie c where c.libelleCategorie= ?1";
		List<Categorie> res = getJpaTemplate().find(query, libelle);
		
		if(res != null) {
			return res.get(0);
		} else {
			return null;
		}
	}
}
